﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;

namespace LifeTree.EntityFrameworkProvider.Tests.Repositories
{
    internal class InMemoryDbSet<T> : IDbSet<T> where T : class
    {
        private HashSet<T> hashSet;
        private IQueryable query;

        public InMemoryDbSet()
        {
            hashSet = new HashSet<T>();
            query = hashSet.AsQueryable();
        }

        public T Add(T entity)
        {
            hashSet.Add(entity);
            return entity;
        }

        public T Attach(T entity)
        {
            hashSet.Add(entity);
            return entity;
        }

        public TDerivedEntity Create<TDerivedEntity>() where TDerivedEntity : class, T
        {
            throw new NotImplementedException();
        }

        public T Create()
        {
            return Activator.CreateInstance<T>();
        }

        public T Find(params object[] keyValues)
        {
            throw new NotImplementedException();
        }

        public System.Collections.ObjectModel.ObservableCollection<T> Local
        {
            get { return new System.Collections.ObjectModel.ObservableCollection<T>(hashSet); }
        }

        public T Remove(T entity)
        {
            hashSet.Remove(entity);
            return entity;
        }

        public IEnumerator<T> GetEnumerator()
        {
            return hashSet.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return hashSet.GetEnumerator();
        }

        public Type ElementType
        {
            get { return query.ElementType; }
        }

        public System.Linq.Expressions.Expression Expression
        {
            get { return query.Expression; }
        }

        public IQueryProvider Provider
        {
            get { return query.Provider; }
        }
    }
}
